﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MetroManagement
{
    /// <summary>
    /// 失物应用服务类
    /// </summary>
    public class FoundAppService : ApplicationService, IFoundAppService
    {
        private readonly IRepository<Found, int> _foundRepository;

        public FoundAppService(
            IRepository<Found, int> foundRepository)
        {
            _foundRepository = foundRepository;
        }

        // 列表
        public async Task<ResultResponse<IList<FoundGroupDto>>> ListAsync()
        {
            // 分组查询
            var list = await _foundRepository
                .ToListAsync();
            var group = list
                .GroupBy(m => m.PublishDate);
            List<Found> foundList;
            List<FoundDto> dtoList;
            var groupList = new List<FoundGroupDto>(group.Count());
            foreach (var item in group)
            {
                foundList = item.ToList();
                dtoList = ObjectMapper.Map<List<Found>, List<FoundDto>>(foundList);
                groupList.Add(new FoundGroupDto()
                {
                    PublishDate = item.Key.ToString("yyyy-MM-dd"),
                    MetroFoundList = dtoList
                });
            }
            return ResultResponse<IList<FoundGroupDto>>.Successful(groupList);
        }

        // 获取
        public async Task<ResultResponse<FoundDto>> GetAsync(int id)
        {
            var found = await _foundRepository.GetAsync(id);
            var dto = ObjectMapper.Map<Found, FoundDto>(found);
            return ResultResponse<FoundDto>.Successful(dto);
        }
    }
}